<script type="text/javascript">
function memProfile(member)
{
	window.location='index.php?members='+member;
}

function funDelMem(userId) {
	var delPro=confirm("Are you sure want to delete merchant?");
    if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("hidDelMember").value=userId;
	    frm.submit();
	}	
}

function funAppMem(userId) {
	var delPro=confirm("Are you sure want to allow this merchant?");
    if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("hidApprove").value=userId;
	    frm.submit();
	}	
}


function funDisAppMem(userId) {
	var delPro=confirm("Are you sure want to block merchant?");
    if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("hidDisApprove").value=userId;
	    frm.submit();
	}	
}

</script>
<?php
require_once '../includes/functions.php';
require_once '../includes/class.phpmailer.php';


$cond = '';
$sort = '';
$ut = $u = $fn = $ln = $dg = 'asc';
if(isset($_GET['sort']))
{
	
	if($_GET['sort']=='user_type')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY user_type ASC';	$sort = '&sort=user_type&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' ORDER BY user_type DESC';	$sort = '&sort=user_type&s_type=desc';	}
	}
	else if($_GET['sort']=='buyers')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' AND user_type=1 ORDER BY username ASC';	$sort = '&sort=buyers&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' AND user_type=1 ORDER BY username DESC';	$sort = '&sort=buyers&s_type=desc';	}
	}
	else if($_GET['sort']=='sellers')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' AND user_type=2 ORDER BY username ASC';	$sort = '&sort=sellers&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' AND user_type=2 ORDER BY username DESC';	$sort = '&sort=sellers&s_type=desc';	}
	}
	else if($_GET['sort']=='merchants')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' AND user_type=3 ORDER BY username ASC';	$sort = '&sort=merchants&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' AND user_type=3 ORDER BY username DESC';	$sort = '&sort=merchants&s_type=desc';	}
	}
	else if($_GET['sort']=='wholesalers')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' AND user_type=5 ORDER BY username ASC';	$sort = '&sort=wholesalers&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' AND user_type=5 ORDER BY username DESC';	$sort = '&sort=wholesalers&s_type=desc';	}
	}
	else if($_GET['sort']=='username')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY username ASC';		$sort = '&sort=username&s_type=asc';	$u='desc';	}
		else
		{	$cond=' ORDER BY username DESC';	$sort = '&sort=username&s_type=desc';	}
	}
	else if($_GET['sort']=='first_name')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY first_name ASC';	$sort = '&sort=first_name&s_type=asc';	$fn='desc';	}
		else
		{	$cond=' ORDER BY first_name DESC';	$sort = '&sort=first_name&s_type=desc';	}
	}
	else if($_GET['sort']=='last_name')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY last_name ASC';	$sort = '&sort=last_name&s_type=asc';	$ln='desc';	}
		else
		{	$cond=' ORDER BY last_name DESC';	$sort = '&sort=last_name&s_type=desc';	}
	}
	else if($_GET['sort']=='created_date')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY created_date ASC';	$sort = '&sort=created_date&s_type=asc'; $dg='desc'; }
		else
		{	$cond=' ORDER BY created_date DESC';	$sort = '&sort=created_date&s_type=desc';}
	}
}

if(!empty($_POST['hidDeny']))
{
	$mId=$_POST['hidDeny'];
	$sqlAppMem="Update tbl_users set denyStatus=1 where user_id='$mId'";
	$rstAppMem=mysql_query($sqlAppMem);
}

if(!empty($_POST['hidApprove']))
{
	$mId=$_POST['hidApprove'];
	$sqlAppMem="Update tbl_users set status=1,denyStatus=0 where user_id='$mId'";
	$rstAppMem=mysql_query($sqlAppMem);	
	$sqlEmail="select * from tbl_emails where email_type='Merchant Approval'";
	
	 $resEmail=mysql_query($sqlEmail);
	 $rowEmail=mysql_fetch_array($resEmail);
	 
	 //user email
	  $sqluseremail="SELECT * FROM tbl_users where user_id='$mId'";				 
	  $rstuseremail=mysql_query($sqluseremail);	
	  $rouseremail=mysql_fetch_array($rstuseremail);
	  $email=$rouseremail['email'];
	 $emailContent=str_replace('##_MerChantUser',$rouseremail['username'],$rowEmail['email_body']);
	  $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
  <tr>
    <td width='18%' height='41'><table width='100%' border='0'>
      <tr>
        <td width='19%' height='68'><img src='".$base_url."images/logo.gif'></td>
        <td width='81%'> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
  
 
</table>
";

		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = $rowEmail['email_subject'];
		$mail->AddAddress($email, $firstName);
		$mail->Send();
}
if(!empty($_POST['delMember']))
{
	$memberId=$_POST['delMember'];
	$sqlDelMem="Delete from tbl_users where user_id=$memberId";
	$rstDelMem=mysql_query($sqlDelMem);	
}
?>
<form name="form1" id="form1" method="post" action="">
			  <input type="hidden" name="hidDelMember" id="hidDelMember">
			  <input type="hidden" name="hidApprove" id="hidApprove">
			  <input type="hidden" name="hidDisApprove" id="hidDisApprove">
			
				
			  
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Denied Members  List</td>
  </tr>
  <tr>
    <td height="577">
	<table width="100%">
            <tbody>
		<tr>
			<td>
				<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr>
						<td width="20%">Member Type</td>
						<td width="30%">
							<select style="width: 120px;" id="user_type" name="user_type">
								<option value="">Select</option>
								<option value="1">Buyer</option>
								<option value="2">Seller</option>
								<option value="3">Merchant</option>
								<option value="4">Seller/Buyer</option>
								<option value="5">Wholeseller</option>
							</select>
							<script type="text/javascript">$('#user_type').val(<?php echo $_POST['user_type']?>)</script>
						</td>
						<td width="20%">Username</td>
						<td width="30%"><input type="text" name="username" id="username" style="width: 150px;" value="<?php echo $_POST['username']?>" /></td>
					</tr>
					<tr>
						<td width="20%">First Name</td>
						<td width="30%">
							<input type="text" name="first_name" id="first_name" style="width: 150px;" value="<?php echo $_POST['first_name']?>" />
						</td>
						<td width="20%">Last Name</td>
						<td width="30%"><input type="text" name="last_name" id="last_name" style="width: 150px;" value="<?php echo $_POST['last_name']?>" /></td>
					</tr>

					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Member" /></td>
					</tr>
				</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<b>Sort  By :</b>
				<a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=user_type&s_type=<?php echo $ut;?>">Member Type</a> &nbsp;&nbsp;&nbsp;
                 <a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=buyers&s_type=<?php echo $ut;?>">Buyers</a> &nbsp;&nbsp;&nbsp;
                 <a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=sellers&s_type=<?php echo $ut;?>">Sellers</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=merchants&s_type=<?php echo $ut;?>">Merchants</a> &nbsp;&nbsp;&nbsp;	
                <a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=wholesalers&s_type=<?php echo $ut;?>">Wholesalers</a> &nbsp;&nbsp;&nbsp;	
				<a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=username&s_type=<?php echo $u;?>">Username</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=first_name&s_type=<?php echo $fn;?>">First name</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=last_name&s_type=<?php echo $ln;?>">Last name</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?denied_members&sort=created_date&s_type=<?php echo $dg;?>">Date Regd.</a>
			</td>
		</tr>
        </tbody>
	</table><br>
	<table width="100%"  border="0" cellspacing="0">      
	<tr class="InnerHeader">       
		<td width="15%" class="line">Member type </td>
		<td width="21%" class="line">Member name </td>
		<td width="21%" class="line">First name </td>
		<td width="16%" class="line">Last name </td>
		
		<td width="9%" class="line">&nbsp;</td>
		<td width="8%" class="line">&nbsp;</td> 
		<td width="10%" class="line">&nbsp;</td>         
	</tr>     
      <?php 				 
	require_once "../includes/pagination.php";
	$itemPerPage=15;
	$search='';
	if(isset($_POST['btnSearchPro'])) {
		
		$username 	= $_POST['username'];		
		$user_type 	= $_POST['user_type'];
		$first_name 	= $_POST['first_name'];
		$last_name 	= $_POST['last_name'];
		
		if($username!='')
		$search.= " AND username='".$username."'";
		
		if($user_type!='')
		$search.=" AND user_type='".$user_type."'";
		
		if($first_name!='')
		$search.="AND first_name='".$first_name."'";
		
		if($last_name!='')
		$search.="AND last_name='".$last_name."'";
		
	}
	$sql_shocat="SELECT * FROM tbl_users where denyStatus=1 $search $cond";				 
	$rst_shocat=mysql_query(getPagingQuery($sql_shocat,$itemPerPage));			
	$numRows = mysql_num_rows($rst_shocat);
	if($numRows>0) {
	       while($row_shocat = mysql_fetch_array($rst_shocat)) {				 
	       $mem_id=$row_shocat['user_id'];
	       $mem_name=$row_shocat['username'];
	       $usr_type=$row_shocat['user_type'];				 
	       $mem_fname=$row_shocat['first_name'];
	       $mem_lname=$row_shocat['last_name'];
	       
	       if($usr_type==1)
		       $mem_type = "Buyer";
	       else if($usr_type==2)
		       $mem_type = "Seller";		 
	       else if($usr_type==3)
		       $mem_type = "Merchant";	
	       else if($usr_type==4)
		       $mem_type = "Seller/ Buyer";
	       else if($usr_type==5)
		       $mem_type = "Wholeseller";
      
	       
	       echo "<tr>";
	       echo "<td class='line'>$mem_type</td>";
	       echo "<td class='line'>$mem_name</td>";
	       echo "<td class='line'>$mem_fname</td>";
	       echo "<td class='line'>$mem_lname</td>";
	       
	       echo "<td height='20' class='line'><input type='button' name='btnApprove' value='Approve' class='button'  onclick='funAppMem($mem_id)'></td> ";
	       echo "<td height='20' class='line'><input type='button' name='view' value='View' class='button'  onclick='memProfile($mem_id)'></td> ";
	       echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='funDelMem($mem_id)';></td> ";
	       echo "</tr>";
	       
	       } 
	} else {
	       echo " <tr><td colspan='7' class='line'>No members found.</td></tr>";
	}				 
	?>      
       </table>
	<div align="center">
		<?php		   
		   $link="newmer";
		   echo $disp=getPagingLink($sql_shocat,$itemPerPage,$link);							   
		?>    
	</div>
	</td>
 </tr>
</table>